/*
 * XCMouse
 * http://code.google.com/p/bit-eclipse/wiki/XCMouse
 *
 * Copyright 2012, Denzel Morris
 * Licensed under the MIT License.
 * See License.txt
 */

#include "ControllerUtility.h"

namespace ControllerUtility {

	bool IsAControllerConnected(XINPUT_STATE* ret) {
		// Return the first available controller
		for( int i = 0; i < 4; ++ i ) {
			const DWORD dwResult = XInputGetState(i, ret);
			if( dwResult == ERROR_SUCCESS ) {
				return true;
			}
		}

		return false;
	}

	bool IsStateSame(const XINPUT_STATE& first, const XINPUT_STATE& second) {
		return first.dwPacketNumber == second.dwPacketNumber;
	}

	void NormalizeAnalogSticks(const XINPUT_STATE& s, float* rlx, float* rly, float* rrx, float* rry) {
		const float lx = (float)s.Gamepad.sThumbLX;
		const float ly = (float)s.Gamepad.sThumbLY;
		const float rx = (float)s.Gamepad.sThumbRX;
		const float ry = (float)s.Gamepad.sThumbLY;

		// Quick and dirty hack, the range isn't completely [-1, 1]; it's [-1,1)
		*rlx = lx / 32768.f;
		*rly = ly / 32768.f;
		*rrx = rx / 32768.f;
		*rry = ry / 32768.f;

		return;
	}
} // namespace ControllerUtility